﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using MedicWeb.MedicService;
using System.Web.UI.HtmlControls;
using System.Collections;
namespace MedicWeb.forms {
    public partial class OrderList : SessionPage {
        protected void Page_Load(object sender, EventArgs e) {
            var mc = new MainService();
            int? prevYear = Session["docListYear"] as int?;
            if (!IsPostBack) {
                for (int i = 2006; i <= DateTime.Now.Year; i++) {
                    comboYear.Items.Add(new ListItem("" + i, "" + i));
                }
                comboYear.SelectedValue = "" + (prevYear ?? DateTime.Now.Year);
            }
            int year = Convert.ToInt32(comboYear.SelectedValue);
            Order[] orderList = mc.GetOrderList(GetSession(), year, false);
            Session["docListYear"] = year;
            foreach (var order in orderList) {
                var row = new TableRow();
                var cellPrint = new TableCell();
                var cellEdit = new TableCell();
                var cellDate = new TableCell();
                var cellNumber = new TableCell();
                var cellProtocol = new TableCell();
                var cellContent = new TableCell();

                cellPrint.Width = 24;
                cellEdit.Width = 24;
                Panel p = new Panel() { CssClass = "print" };
                p.Controls.Add(
                    new HyperLink() {
                        NavigateUrl = ResolveUrl("~/forms/Reports/Print/Print.aspx?type="+typeof(PrintOrder)+"&id=" + order.Id),
                        CssClass = "print",
                        Text = "Приказ"
                    });
                p.Controls.Add(
                    new HyperLink() {
                        NavigateUrl = ResolveUrl("~/forms/Reports/Order/DoctorListReport.aspx?id=" + order.Id),
                        CssClass = "print",
                        Text = "Список"
                    });
                cellPrint.Controls.Add(p);
                cellEdit.Controls.Add(
                    new HyperLink() {
                        NavigateUrl = ResolveUrl("~/forms/Editors/OrderEdit.aspx?id=" + order.Id),
                        CssClass = "modal open"
                    });
                cellDate.Text = order.Date.ToShortDateString();
                cellNumber.Text = order.Number;
                string date = order.ProtocolDate != null ? order.ProtocolDate.Value.ToShortDateString() : "&nbsp;&nbsp;&nbsp;";
                string number = !string.IsNullOrEmpty(order.ProtocolNumber) ? "№&nbsp;" + order.ProtocolNumber : "&nbsp;&nbsp;&nbsp;";
                cellProtocol.Text = number + "&nbsp;" + date;
                cellContent.Text = order.Content;

                row.Cells.Add(cellPrint);
                row.Cells.Add(cellEdit);
                row.Cells.Add(createDeleteCell(order));
                row.Cells.Add(cellDate);
                row.Cells.Add(cellNumber);
                row.Cells.Add(cellProtocol);
                row.Cells.Add(cellContent);

                TableOrders.Rows.Add(row);
            }
        }
    }

}